#include "button.h"
#include <FreeRTOS.h>
#include <task.h>
#include <stdio.h>
#include <bl_gpio.h>
#include <hal_sys.h>
#include "file_op.h"

#define GPIO_BUTTON_PIN 8

static void button_task(void *args)
{
    int ret;
    unsigned long long count = 0;

    bl_gpio_enable_input(GPIO_BUTTON_PIN, 0, 0);
    while (1) {
        ret = bl_gpio_input_get_value(GPIO_BUTTON_PIN);
        if (ret == 1) {
            count++;
        } else {
            count = 0;
        }
        vTaskDelay(20);

        if (count > 500) {
            remove_file("config.json");
            remove_file("timer.json");
            printf("CCCCCCCCCCCCCCCCCCCCCCCCCClear.\r\n");
            count = 0;
            hal_reboot();
        }
    }
}

void init_button(void)
{
    xTaskCreate(button_task, (char *)"Button task", 128, NULL, 5, NULL);
}